package uk.ac.aber.paws.server.config;

import java.io.*;

import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;

import uk.ac.aber.paws.server.core.PAWSServer;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 21st May 2012
 * 
 * ConfigManager.java
 * 
 * Loads and stores the configuration data for the application.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class ConfigManager {

	private static Properties properties = null;
	
	/**
	 * Loads the properties from the config file
	 * @param configPath The path to the config file, whether it be within the JAR or standalone file
	 * @param inJar If true, then the config file is contained within the JAR, if false, then standalone file on local drive
	 */
	public static void loadProperties(String configPath, boolean inJar) {

		try {
	        properties = new Properties();
	  
	        if (configPath == null) {
	    		URL url = Thread.currentThread().getContextClassLoader().getResource("");
	 
	                if (url != null) {
						properties.load(url.openStream());
	                }
	            } 
	            else {
	                
	            	InputStream is = null;
	            	
	            	if (inJar) {
		            	// loads the config.properties file
	            		is = ConfigManager.class.getClassLoader().getResourceAsStream(configPath);
	            	}
		        	else {
		        		is = new FileInputStream(configPath);
		        	}
	        	
	        	if (is != null) {
	        		properties.load(is);
	        	}
	        	else {
	        		PAWSServer.LOG_FILE.log(Level.SEVERE,"Failed to load application config file");
	        	}
	        }
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			PAWSServer.LOG_FILE.log(Level.ALL,e.toString());
		}
    }
	
	/**
	 * Returns the value for the given property name
	 * @param name The name of the config property
	 * @return The value for the config property
	 * @throws IOException 
	 */
	public static String getProperty(String name) {

        if (properties == null) {
           
			loadProperties(null,true);
        }	
 
        return (properties.getProperty(name)==null) ? Constants.UNDEFINED : properties.getProperty(name).trim();
    }
	
	/**
	 * Sets the given configuration property
	 * @param property The config property
	 * @param value The value to be set for this property
	 */
	public static void setProperty(String property, String value) {
		
		properties.setProperty(property, value);
	}
	
	/**
	 * Returns all config properties stored in this object
	 * @return All properties
	 */
	public static Properties getAllProperties() {
		return properties;
	}
}

